<template>
  <div>
    <nav-text><span slot="navText">当前位置: 数据中心 > 设置 > 用户设置</span></nav-text>
    <!-- 搜索框 -->
    <div class="searchBox">
      <div>
        <textBox>
          <span slot="titleName">姓 名 :</span>
          <input
            v-model="Names"
            slot="textBox"
            class="textBoxSize"
            type="text"
            placeholder="请输入姓名"
          />
        </textBox>
      </div>
      <div>
        <selectBox>
          <span slot="titleName">大 区 :</span>
          <el-select
            slot="selectBox"
            v-model="Regions"
            clearable
            placeholder="请选择大区"
          >
            <el-option
              v-for="item in regionOption"
              :key="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </selectBox>
      </div>
      <div>
        <selectBox>
          <span slot="titleName">状态:</span>
          <el-select
            slot="selectBox"
            v-model="addBoxStateValue"
            clearable
            placeholder="请选择运行状态"
          >
            <el-option
              v-for="item in addBoxState"
              :key="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </selectBox>
      </div>
      <div class="layoutBtn">
        <div>
          <search-button>
            <el-button
              @click.stop="userSetList('1',Names,Regions,Status)"
              slot="searchBtn"
              type="success"
              size="mini"
            >搜 索</el-button>
          </search-button>
        </div>
        <div>
          <download-button>
            <el-button
              @click.stop="showaddBox"
              slot="dounloadBtn"
              type="primary"
              icon='el-icon-plus'
              circle
            ></el-button>
          </download-button>
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table
        :header-cell-style="headStyle"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        border
        :data="tableData"
        :fit="fit"
        :highlight-current-row='highlight'
      >
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="dataId"
          label="序 号"
          min-width="40"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="userName"
          label="账 号"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="name"
          label="姓 名"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="roleIds"
          label="权 限"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="aNameCode"
          label="大 区"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="isUsing"
          label="状 态"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="editTime"
          label="更 新 时 间"
        >
        </el-table-column>

        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="principal"
          label="操 作"
        >
          <template slot-scope="scope">
            <el-button
              @click.stop="showAlterBox(scope.row)"
              class="el-button-details"
              size="medium "
              type="text"
            >修 改</el-button>
            <el-button
              @click.stop="showDeleteBox(scope.row)"
              class="el-button-delete"
              size="medium "
              type="text"
            >删 除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器 -->
    <pagination>
      <el-pagination
        @current-change="pageNumLink($event,Names,Regions,Status)"
        slot="pagination"
        background
        layout="prev, pager, next, jumper"
        :total="pageTotal"
        :page-size="pageSize"
        hide-on-single-page
      >
      </el-pagination>
    </pagination>
    <!-- 删除弹框 -->
    <delete-box v-if="deleteBoxStates">
      <button
        slot="cancel"
        @click.stop='cancelDeleteBox'
      >取 消</button>
      <button
        slot="affirm"
        @click.stop="affirmDeleteBox"
      >确 认</button>
    </delete-box>
    <!-- 新增数据弹框 isShowAddData -->
    <baseDataBox v-if="isShowAddData">
      <span slot="title">新 增 数 据</span>
      <li slot="content">
        <div>
          <baseInput>
            <label
              for=""
              slot="label"
            >姓 名 :</label>
            <input
              v-model="addBoxNameValue"
              slot="input"
              class="inputTextStyle"
              placeholder="请输入姓名"
              type="text"
            />
          </baseInput>
        </div>
        <div>
          <whiteSelectBox>
            <span
              class="stateText"
              slot="titleName"
            >大 区 :</span>
            <el-select
              class="selectBoxStyle"
              slot="selectBox"
              v-model="addBoxAreaValue"
              clearable
              placeholder="请选择大区"
            >
              <el-option
                v-for="item in addBoxArea"
                :key="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </whiteSelectBox>
        </div>
      </li>
      <li slot="content">
        <div>
          <whiteSelectBox>
            <span
              class="stateText"
              slot="titleName"
            >权 限 :</span>
            <el-select
              class="selectBoxStyle"
              slot="selectBox"
              v-model="addBoxPowerValue"
              clearable
              placeholder="用户权限，数值越大权限越大"
            >
              <el-option
                v-for="item in addBoxPower"
                :key="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </whiteSelectBox>
        </div>
        <div>
          <whiteSelectBox>
            <span
              class="stateText"
              slot="titleName"
            >状态 :</span>
            <el-select
              class="selectBoxStyle"
              slot="selectBox"
              v-model="addBoxStateValue"
              clearable
              placeholder="请选择大区"
            >
              <el-option
                v-for="item in addBoxState"
                :key="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </whiteSelectBox>
        </div>
      </li>
      <li slot="content">
        <div>
          <baseInput>
            <label
              for=""
              slot="label"
            >账 号 :</label>
            <input
              v-model="addBoxID"
              slot="input"
              class="inputTextStyle"
              placeholder="请输入账号"
              type="text"
            />
          </baseInput>
        </div>
        <div>
        </div>
      </li>
      <button
        slot="delete"
        @click.stop="cancelAddBox"
      >取 消</button>
      <button
        slot="affirm"
        @click.stop="affirmAddBox"
      >确 认</button>
    </baseDataBox>
    <!-- 修改数据弹窗 -->
    <baseDataBox v-if="isShowAlterData">
      <span slot="title">修 改 数 据</span>
      <li slot="content">
        <div>
          <baseInput>
            <label
              for=""
              slot="label"
            >姓 名 :</label>
            <input
              v-model="alterBoxNameValue"
              slot="input"
              class="inputTextStyle"
              placeholder="请输入姓名"
              type="text"
            />
          </baseInput>
        </div>
        <div>
          <whiteSelectBox>
            <span
              class="stateText"
              slot="titleName"
            >大 区 :</span>
            <el-select
              class="selectBoxStyle"
              slot="selectBox"
              v-model="alterBoxAreaValue"
              clearable
              placeholder="请选择大区"
            >
              <el-option
                v-for="item in addBoxArea"
                :key="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </whiteSelectBox>
        </div>
      </li>
      <li slot="content">
        <div>
          <whiteSelectBox>
            <span
              class="stateText"
              slot="titleName"
            >权 限 :</span>
            <el-select
              class="selectBoxStyle"
              slot="selectBox"
              v-model="alterBoxPowerValue"
              clearable
              placeholder="请选择权限"
            >
              <el-option
                v-for="item in addBoxPower"
                :key="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </whiteSelectBox>
        </div>
        <div>
          <whiteSelectBox>
            <span
              class="stateText"
              slot="titleName"
            >状态 :</span>
            <el-select
              class="selectBoxStyle"
              slot="selectBox"
              v-model="alterBoxStateValue"
              clearable
              placeholder="请选择状态"
            >
              <el-option
                v-for="item in addBoxState"
                :key="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </whiteSelectBox>
        </div>
      </li>
      <button
        slot="delete"
        @click.stop="cancelAlterBox"
      >取 消</button>
      <button
        slot="affirm"
        @click.stop="affirmAlterBox"
      >确 认</button>
    </baseDataBox>
  </div>
</template>

<script>
import whiteSelectBox from "@/components/DataCenter/BaseForm/whiteSelectBox";
import { userSetList } from "@/request/api.js";
import { addUserSetData } from "@/request/api.js";
import { alterUserSetData } from "@/request/api.js";
import { userSetDelete } from "@/request/api.js";
import navText from "@/components/DataCenter/MyLocation/myLocation";
import textBox from "@/components/DataCenter/BaseForm/textBox";
import searchButton from "@/components/DataCenter/BaseForm/searchBtn";
import downloadButton from "@/components/DataCenter/BaseForm/downloadBtn";
import selectBox from "@/components/DataCenter/BaseForm/selectBox";
import dataSelectBox from "@/components/DataCenter/BaseForm/dateSelectBox";
import pagination from "@/components/baseCpn/pagination.vue";

import deleteBox from "@/components/DataCenter/BaseForm/deleteBox.vue";
import baseDataBox from "@/components/DataCenter/BaseForm/baseDataBox";
import baseInput from "@/components/DataCenter/BaseForm/baseInput";
import "@/assets/base/dataCenterTableStyle.css";
// import { from } from "webpack-sources/lib/CompatSource";
export default {
  components: {
    id: "1",
    navText,
    textBox,
    searchButton,
    downloadButton,
    selectBox,
    dataSelectBox,
    pagination,
    deleteBox,
    baseDataBox,
    baseInput,
    whiteSelectBox
  },
  mounted() {
    this.userSetList(this.$page);
    this.regionOption = localStorage.getItem("allArea").split(",");
    this.addBoxArea = localStorage.getItem("allArea").split(",");
  },
  data() {
    return {
      userID: "",
      pageTotal: null,
      pageSize: 8,
      isShowAddData: false,
      isShowAlterData: false,
      deleteBoxStates: false,
      highlight: true,
      fit: true,
      headStyle: {
        height: "7rem",
        backgroundColor: "#131428",
        color: "#FFFFFF",
        border: "1px solid #792a4c",
        fontSize: "1.9rem"
      },
      cellStyle: {
        height: "7rem",
        border: "1px solid #792a4c",
        fontSize: "1.8rem"
      },
      tableData: [],
      Names: "",
      Regions: "",
      regionOption: [],
      // 添加弹框属性
      addBoxNameValue: "",
      addBoxAreaValue: "",
      addBoxArea: [],
      addBoxPowerValue: "",
      addBoxPower: ["2", "3", "4"],
      addBoxStateValue: "",
      addBoxState: ["启用", "禁用"],
      addBoxID: "",
      //修改弹窗属性
      alterBoxNameValue: "",
      alterBoxAreaValue: "",
      alterBoxPowerValue: "",
      alterBoxStateValue: "",
      //删除弹框属性
      deleteBoxID: null
    };
  },
  methods: {
    pageNumLink(p, Names, Regions, Status) {
      var page = String(p);
      if (p) {
        this.userSetList(page, Names, Regions, Status);
      }
    },
    // 获取用户接口
    userSetList(page, Names, Regions, Status) {
      console.log(page, Names, Regions, Status);
      userSetList({
        page: page,
        pageSize: this.$pageSize,
        name: Names,
        aName: Regions,
        isUsing: Status
      })
        .then(res => {
          if (res.data.success) {
            console.log("用户设置列表请求成功", res.data.total);
            this.tableData = res.data.rows;
            // 表格序号内容
            this.pageTotal = res.data.total;
            for (var i = 0; i < res.data.rowcount; i++) {
              var k = (page - 1) * 8 + i + 1;
              this.tableData[i].dataId = k;
            }
          } else {
            this.$message.error({
              message: res.data.errorMsg
            });
          }
        })
        .catch(err => {
          console.log("用户设置列表请求失败", err);
        });
    },
    showaddBox() {
      this.isShowAddData = true;
    },
    cancelAddBox() {
      this.isShowAddData = false;
    },
    affirmAddBox() {
      this.isShowAddData = false;
      let addBoxID = this.addBoxID;
      let addBoxName = this.addBoxNameValue;
      let addBoxPower = this.addBoxPowerValue;
      let addBoxArea = this.addBoxAreaValue;
      let addBoxState = this.addBoxStateValue == "启用" ? "1" : "0";
      this.addUserSetData(
        addBoxID,
        addBoxName,
        addBoxPower,
        addBoxArea,
        addBoxState
      );
    },

    showDeleteBox(e) {
      this.deleteBoxStates = true;
      console.log(e);
      this.deleteBoxID = e.id;
    },
    cancelDeleteBox() {
      this.deleteBoxStates = false;
    },
    affirmDeleteBox() {
      this.deleteBoxStates = false;
      //删除单条数据接口
      userSetDelete({
        userId: this.deleteBoxID,
        isUsing: "-1"
      })
        .then(res => {
          console.log("删除数据接口成功", res);
          this.userSetList(this.$page);
          if (res.data.success) {
            this.$message.success({
              message: "删除数据成功"
            });
          }else {
            this.$message.error({
              message: res.data.errorMsg
            });
          }
        })
        .catch(err => {
          console.log("删除数据接口失败", err);
        });
    },
    showAlterBox(e) {
      console.log("e是", e);
      this.userID = e.id;
      this.isShowAlterData = true;
      this.alterBoxNameValue = e.name;
      this.alterBoxPowerValue = e.roleIds;
      this.alterBoxAreaValue = e.aName;
      this.alterBoxStateValue = e.isUsing;
    },
    cancelAlterBox() {
      this.isShowAlterData = false;
    },
    affirmAlterBox() {
      this.isShowAlterData = false;
      // 修改单行数据的接口
      console.log(this.userID);
      alterUserSetData({
        userId: this.userID,
        name: this.alterBoxNameValue,
        roleIds: this.alterBoxPowerValue,
        aCode: this.alterBoxAreaValue,
        isUsing: this.alterBoxStateValue
      })
        .then(res => {
          console.log("修改数据成功！", res);
          if (res.data.success) {
            this.$message.success({
              message: "修改数据成功！"
            });
            this.userSetList(this.$page);
          } else {
            this.$message.error({
              message: res.data.errorMsg
            });
          }
        })
        .catch(err => {
          console.log("修改数据失败", err);
        });
    },

    rowStyle(row) {
      if (row.rowIndex % 2 === 0) {
        return { backgroundColor: "#222644" };
      } else if (row.rowindex % 2 === 1) {
        return { backgroundColor: "#262b4b" };
      }
    },
    //新增数据接口
    addUserSetData(BoxID, BoxName, BoxPower, BoxArea, BoxState) {
      addUserSetData({
        userName: BoxID,
        name: BoxName,
        roleIds: BoxPower,
        aCode: BoxArea,
        isUsing: BoxState == "启用" ? "1" : "0"
      })
        .then(res => {
          if (res.data.success) {
            console.log("添加数据成功", res);
            this.$message.success({
              message: "新增数据成功"
            });
            this.userSetList(this.$page); //刷新列表数据
            this.$message.success({
              message: res.data.errorMsg
            });
          }
        })
        .catch(err => {
          console.log("添加数据失败", err);
        });
    }
  }
};
</script>

<style lang='scss' scoped>
.searchBox {
  width: 100%;
  height: 16rem;
  display: flex;
  div {
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  .layoutBtn {
    display: flex;
    flex-direction: row;
    div {
      flex: 1;
      display: flex;
    }
  }
}
.tableBox {
  width: 95%;
  height: 63.5rem;
  margin: 0 auto;
  padding-top: 2rem;
  overflow: hidden;
  .el-button-details {
    font-size: 1.6rem;
    color: #33ffff;
    margin-right: 0.4rem;
  }
  .el-button-delete {
    font-size: 1.6rem;
    color: #ff9cc6;
  }
}
</style>